﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            var query = textBox1.Text;
            var sqlquery = string.Format("select * from Students where Name like'%{0}%'", query);
            var sdf = DbHelper.GetdataTable(sqlquery);
            dataGridView1.DataSource = sdf;
        }
        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            Editform editform = new Editform();
            var res = editform.ShowDialog();

            if (res == DialogResult.Yes)
            {
                string codString = "select * from Students";
                var dataTable = DbHelper.GetdataTable(codString);
                dataGridView1.DataSource = dataTable;
            }
            else
            {

            }
        }
        //修改
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["id"].Value;
                var Name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
                var Age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
                var Score = (string)dataGridView1.SelectedRows[0].Cells["Score"].Value;
                Editform editform = new Editform(id, Name, Age, Score);
                var res = editform.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    string codString = "select * from Students";
                    var dataTable = DbHelper.GetdataTable(codString);
                    dataGridView1.DataSource = dataTable;

                }
                else
                {

                }

            }
            else
            {
                MessageBox.Show("请选择需要更改的行", "提示");
            }
        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {

            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["id"].Value;
                var res = MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);


                if (res.Equals(DialogResult.Yes))
                {

                    var str = string.Format("delete from Students where id = {0}", id);
                    var count = DbHelper.AddOrEditSave(str);


                    if (count > 0)
                    {
                        MessageBox.Show("删除成功", "提示");
                        string codString = "select * from course";
                        var dataTable = DbHelper.GetdataTable(codString);
                        dataGridView1.DataSource = dataTable;
                    }
                }
            }
            else
            {
                MessageBox.Show("你没有选中任何数据", "提示");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string com = "server =LS--20170312VIZ ; uid =liyuncai ;pwd =liyun916113 ; database =Schools ";
            SqlConnection sqlConnection = new SqlConnection(com);
            string sel = "select * from Students";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sel,sqlConnection);
            DataTable dataTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(dataTable);
            dataGridView1.DataSource = dataTable;
            dataGridView1.ReadOnly = true;



        }
    }
}
